package com.jcfk.eam.service.board;

import com.jcfk.eam.dao.board.MoldTaskBoardDao;
import com.jcfk.eam.domain.dto.board.MouldMaintenanceTaskBoardDTO;
import com.jcfk.eam.domain.dto.board.MouldProductionTaskBoardDTO;
import com.jcfk.eam.domain.dto.board.MouldStatusBoardDTO;
import com.jcfk.eam.domain.dto.board.MouldStatusBoardDetailDTO;
import com.jcfk.eam.domain.query.board.MouldMaintenanceTaskQuery;
import com.jcfk.eam.domain.query.board.MouldProductionTaskQuery;
import com.jcfk.eam.domain.query.board.MouldStatusBoardQuery;
import com.jcfk.eam.util.BigDecimalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MoldTaskBoardService {
    @Autowired
    private MoldTaskBoardDao moldTaskBoardDao;

    /**
     * 模具生产任务看板
     */
    public List<MouldProductionTaskBoardDTO> productionTask(MouldProductionTaskQuery query) {
        LocalDate localDate = LocalDate.now();
        Date endDate = Date.from(localDate.plusDays(2).atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date startDate = Date.from(localDate.minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());

        List<MouldProductionTaskBoardDTO> moldProductionTaskBoardDTOList =
                moldTaskBoardDao.productionTaskList(startDate, endDate,query);
        moldProductionTaskBoardDTOList.forEach(item -> {
            if (item.getCumulativeUseTimes() != null) {
                item.setUseProgress(BigDecimalUtils.ratio(item.getCumulativeUseTimes(), item.getAssetLifespan()));
            }
        });
        return moldProductionTaskBoardDTOList;
    }


    /**
     * 模具维修任务看板
     *
     * @return
     */
    public List<MouldMaintenanceTaskBoardDTO> maintenanceTask(MouldMaintenanceTaskQuery query) {
        LocalDate localDate = LocalDate.now();
        Date endDate = Date.from(localDate.plusDays(2).atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date startDate = Date.from(localDate.minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
        List<MouldMaintenanceTaskBoardDTO> result = moldTaskBoardDao.maintenanceTaskList(startDate, endDate,query);
        return result;
    }

    /**
     * 模具任务看板
     *
     * @return
     */
    public List<MouldStatusBoardDTO> mouldStatus(MouldStatusBoardQuery requestQuery) {
        List<MouldStatusBoardDTO> mouldStatusBoardDTOList = new ArrayList<>();
        List<MouldStatusBoardDetailDTO> mouldStatusBoardDetailDTOList = moldTaskBoardDao.mouldStatusList(requestQuery.getProjectCode(),requestQuery.getStatusList());
        Map<Integer, List<MouldStatusBoardDetailDTO>> integerListMap = mouldStatusBoardDetailDTOList.stream().peek(item -> {
            item.setRatio(BigDecimalUtils.ratio(item.getMouldUseCount(), item.getMouldLife()));
        }).collect(Collectors.groupingBy(MouldStatusBoardDetailDTO::getStatus));
        for (Integer key : integerListMap.keySet()) {
            MouldStatusBoardDTO mouldStatusBoardDTO = new MouldStatusBoardDTO();
            mouldStatusBoardDTO.setStatus(key);
            List<MouldStatusBoardDetailDTO> mouldStatusBoardDetailDTOS = integerListMap.get(key);
            mouldStatusBoardDTO.setCount(mouldStatusBoardDetailDTOS.size());
            mouldStatusBoardDTO.setDetails(mouldStatusBoardDetailDTOS);
            mouldStatusBoardDTOList.add(mouldStatusBoardDTO);
        }
        return mouldStatusBoardDTOList;
    }
}
